<?php
namespace app\index\controller;
use think\facade\View;
use think\facade\Db;
use think\facade\Request;
class InfoView extends Base{
    public function __construct(\think\App $app) {
        parent::__construct($app);         
        $this->catid=Request::param("catid",0,"intval");
        $cat=Db::name("cat")->find($this->catid);
        if(!$cat){
            $this->error("栏目信息不存在");
        } 
        $cat_data=Db::name("cat_data")->find($this->catid);
        $this->cat=array_merge($cat,$cat_data);        
        //栏目字段赋值
        View::assign("cat",$this->cat);        
        //
        if(!$this->cat){
            $this->error("分类不存在！");
        }      
        if(!$this->cat['status']){
            $this->error("栏目已禁用！");
        }          
        //当前栏目对应的模型信息
        $this->ModelInfo=$GLOBALS['model'][$this->cat['modelid']];
        $this->tableName="cms_".$this->ModelInfo['table_name'];//主表名
        $this->tableDataName="cms_".$this->ModelInfo['table_name']."_data";//副表名
        //信息
        $this->id=Request::param("id",0,"intval");
        $zb_r=Db::name($this->tableName)->find($this->id);
        $fb_r=Db::name($this->tableDataName)->find($this->id);
        $this->r=array_merge($zb_r,$fb_r);
        //判断是否有站外链接，如果有跳转链接直接跳转过去
        if(strstr($this->r["titleurl"],"http://")){
            redirect($this->r["titleurl"]);
            exit;
        }             
        if(!$this->r){
            $this->error("信息不存在！");
        }
        $this->r['titleurl']=titleurl($this->r);//标题链接
        View::assign($this->r);
        //点击量+1
        Db::name($this->tableName)->where('id', $this->id)->inc('views')->update();
        //验证访问权限验证
        if($this->cat['member_group_id']){
            //没有登录
            if(!$this->member_id){
                $this->error("请先登录",url("Public/login"));
            }
            //登录了，但是权限不够
            if(!strstr($this->cat['member_group_id'],"|".$this->member['group_id']."|")){
                $this->error("当前会员组没有权限浏览");
            }
        }   
        //tdk
        View::assign("pagetitle",$this->r['pagetitle']?$this->r['pagetitle']:$this->r['title']);//pagetitle
        View::assign("keywords",$this->r['keywords']?$this->r['keywords']:$this->r['title']);//关键词
        View::assign("description",$this->r['description']?$this->r['description']:$this->r['title']);//描述
    }
    public function index(){
        //
        View::assign("preinfo",$this->preinfo()); //上一篇
        View::assign("nextinfo",$this->nextinfo()); //下一篇
        //内容模板
        if($this->r["viewtemp"]){
            $viewtemp=$this->r["viewtemp"];
        }else{
            $viewtemp=$this->cat["viewtemp"];
        }
        return View::fetch(base_path().'data/templateFile/'.$viewtemp.".html");
    }
    //上一篇
    protected function preinfo(){
        $where=[];
        $where[]=['catid','=',$this->catid];
        $where[]=['id','<>',$this->id];
        if($this->r['newstime']){
            $where[]=['newstime','>=',$this->r['newstime']];    
        }
        $data=Db::name($this->tableName)->where($where)->field("title,id,catid")->order('newstime asc,id asc')->find();
        if($data){
            $titleurl=titleurl($data);
            $link="<a href=\"".$titleurl."\">".$data[title]."</a>";
        }else{
            $link="没有了";
        }
        return $link;        
    }
    //获取点击数
    public function getClick(){
        echo "document.write('".$this->r["views"]."');";exit;
    }    
    //下一篇
    protected function nextinfo(){
        $where=[];
        $where[]=['catid','=',$this->catid];
        $where[]=['id','<>',$this->id];
        if($this->r['newstime']){
            $where[]=['newstime','<=',$this->r['newstime']];    
        }           
        $data=Db::name($this->tableName)->where($where)->field("title,id,catid")->order('newstime desc,id desc')->find();
        if($data){
            $titleurl=titleurl($data);
            $link="<a href=\"".$titleurl."\">".$data[title]."</a>";
        }else{
            $link="没有了";
        }
        return $link;         
    }
}